/*
 * Created by JFormDesigner on Mon Jul 06 13:57:13 BST 2009
 */

package gui;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import recordsDatabase.CategoryDetails;
import recordsDatabase.ListOfPersons;
import recordsDatabase.Person;
import recordsDatabase.Record;
import resources.DateUtilities;
import resources.FileReaderWriter;

/**
 * @author alex ouzounis
 */
public class WindowRecords extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5395902636524650494L;
	private DateUtilities dateTime = new DateUtilities();
	private Person person=null;

	public WindowRecords() {
		initComponents();
		initRecordsList();
	}

	public WindowRecords(Person p) {
		initComponents();
		person = p;
		initPerson();
		setTitle("Record: "
				+ person.getRecord("Name").getCategoryDetailsList().get(0)
						.getField());
	}

	private void initRecordsList() {
		for (Person p : ListOfPersons.getInstance().getPersonsList()) {
			modelRecords.addElement(p.getRecord("Name")
					.getCategoryDetailsList().get(0).getField());
		}
	}

	private void initPerson() {
		modelRecords.addElement(person.getRecord("Name")
				.getCategoryDetailsList().get(0).getField());
	}

	private void listRecordsValueChanged(ListSelectionEvent e) {
		modelRecordFields.clear();
		modelFieldDetails.clear();
		String value = (String) listRecords.getSelectedValue();
		for (Record r : ListOfPersons.getInstance().getPerson(value)
				.getRecordsList()) {
			modelRecordFields.addElement(r.getRecordName());
		}
	}

	private void listRecordFieldsValueChanged(ListSelectionEvent e) {
		if (listRecordFields.getSelectedIndex() != -1) {
			modelFieldDetails.clear();
			String value = (String) listRecords.getSelectedValue();
			String value2 = (String) listRecordFields.getSelectedValue();
			for (CategoryDetails cd : ListOfPersons.getInstance().getPerson(
					value).getRecord(value2).getCategoryDetailsList()) {
				modelFieldDetails.addElement(cd.getField());
			}
		}

	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - alex ouzounis
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		splitPane1 = new JSplitPane();
		splitPane2 = new JSplitPane();
		panel1 = new JPanel();
		label1 = new JLabel();
		scrollPane1 = new JScrollPane();
		modelRecords = new DefaultListModel();
		listRecords = new JList(modelRecords);
		panel2 = new JPanel();
		label2 = new JLabel();
		scrollPane2 = new JScrollPane();
		modelRecordFields = new DefaultListModel();
		listRecordFields = new JList(modelRecordFields);
		panel3 = new JPanel();
		label3 = new JLabel();
		scrollPane3 = new JScrollPane();
		modelFieldDetails = new DefaultListModel();
		listFieldDetails = new JList(modelFieldDetails);
		buttonExport = new JButton();

		// ======== this ========
		setVisible(true);
		setTitle("Records");
		setClosable(true);
		setResizable(true);
		setMaximizable(true);
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		// ======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));

			dialogPane.setLayout(new BorderLayout());

			// ======== contentPanel ========
			{
				contentPanel.setLayout(new BorderLayout());

				// ======== splitPane1 ========
				{
					splitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);

					// ======== splitPane2 ========
					{

						// ======== panel1 ========
						{
							panel1.setLayout(new BorderLayout());

							// ---- label1 ----
							label1.setText("Select a Person:");
							panel1.add(label1, BorderLayout.NORTH);

							// ======== scrollPane1 ========
							{

								// ---- listRecords ----
								listRecords
										.addListSelectionListener(new ListSelectionListener() {
											public void valueChanged(
													ListSelectionEvent e) {
												listRecordsValueChanged(e);
											}
										});
								scrollPane1.setViewportView(listRecords);
							}
							panel1.add(scrollPane1, BorderLayout.CENTER);
						}
						splitPane2.setLeftComponent(panel1);

						// ======== panel2 ========
						{
							panel2.setLayout(new BorderLayout());

							// ---- label2 ----
							label2.setText("Select a Field:");
							panel2.add(label2, BorderLayout.NORTH);

							// ======== scrollPane2 ========
							{

								// ---- listRecordFields ----
								listRecordFields
										.addListSelectionListener(new ListSelectionListener() {
											public void valueChanged(
													ListSelectionEvent e) {
												listRecordFieldsValueChanged(e);
											}
										});
								scrollPane2.setViewportView(listRecordFields);
							}
							panel2.add(scrollPane2, BorderLayout.CENTER);
						}
						splitPane2.setRightComponent(panel2);
					}
					splitPane1.setTopComponent(splitPane2);

					// ======== panel3 ========
					{
						panel3.setLayout(new BorderLayout());

						// ---- label3 ----
						label3.setText("Field Details:");
						panel3.add(label3, BorderLayout.NORTH);

						// ======== scrollPane3 ========
						{
							scrollPane3.setViewportView(listFieldDetails);
						}
						panel3.add(scrollPane3, BorderLayout.CENTER);
						buttonExport.setText("Export Record(s)");
						buttonExport.addMouseListener(new MouseAdapter() {
							@Override
							public void mouseClicked(MouseEvent e) {
								ExportButtonMouseClicked(e);
							}
						});
						panel3.add(buttonExport, BorderLayout.SOUTH);
					}
					splitPane1.setBottomComponent(panel3);
				}
				contentPanel.add(splitPane1, BorderLayout.CENTER);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		// JFormDesigner - End of component initialization
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - alex ouzounis
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JSplitPane splitPane1;
	private JSplitPane splitPane2;
	private JPanel panel1;
	private JLabel label1;
	private JScrollPane scrollPane1;
	private JList listRecords;
	private JPanel panel2;
	private JLabel label2;
	private JScrollPane scrollPane2;
	private JList listRecordFields;
	private JPanel panel3;
	private JLabel label3;
	private JScrollPane scrollPane3;
	private JList listFieldDetails;
	private DefaultListModel modelRecords;
	private DefaultListModel modelRecordFields;
	private DefaultListModel modelFieldDetails;
	private JButton buttonExport;

	// JFormDesigner - End of variables declaration //GEN-END:variables

	private void ExportButtonMouseClicked(MouseEvent e) {

		StringBuilder text = new StringBuilder();
		String newLine = "\n";
		text.append("DataMiner Records Export");
		text.append(newLine);
		text.append(dateTime.getDateTime("yyyy.MMMMM.dd GGG hh:mm aaa"));
		text.append(newLine);
		text.append("===================================================");
		text.append(newLine);
		if (person != null) {
			for (Record r : person.getRecordsList()) {
				text.append(newLine);
				text.append("------------");
				text.append(newLine);
				text.append("Category: " + r.getRecordName() + newLine);
				text.append("Field(s):" + newLine);
				for (CategoryDetails cd : r.getCategoryDetailsList()) {
					text.append(cd.getField());
					text.append(newLine);
				}

			}
		} else {
			int i = 1;
			for (Person p : ListOfPersons.getInstance().getPersonsList()) {
				text.append(newLine);
				text.append("------------");
				text.append(newLine);
				text.append("Record: " + i);
				text.append(newLine);
				for (Record r : p.getRecordsList()) {
					text.append(newLine);
					text.append("Category: " + r.getRecordName() + newLine);
					text.append("Field(s):" + newLine);
					for (CategoryDetails cd : r.getCategoryDetailsList()) {
						text.append(cd.getField());
						text.append(newLine);
					}
				}
				i++;
			}
		}

		// create a JFileChooser with its appropriate properties
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		int fileVal = fileChooser.showSaveDialog(buttonExport);

		// check if the cancel button was pressed
		if (fileVal == JFileChooser.CANCEL_OPTION) {

		} else {
			// if everything is ok then get the selected file from the user
			File fileName = fileChooser.getSelectedFile();

			// if the file is empty display an error message
			if (fileName == null || fileName.getName().equals("")) {
				JOptionPane.showMessageDialog(buttonExport,
						"Please enter a correct filename.", "Error",
						JOptionPane.ERROR_MESSAGE);
			} else

			// if the path name is OK, call the loader!
			{
				try {
					String newFileName = fileName.getName();
					FileReaderWriter fileWrite = new FileReaderWriter();
					if (!fileName.getName().contains(".txt")) {
						newFileName = newFileName + ".txt";
					}
					fileWrite.WriteFile(newFileName, text.toString());
					JOptionPane.showMessageDialog(buttonExport,
							"Records exported.", "Success",
							JOptionPane.INFORMATION_MESSAGE);
				} catch (Exception e1) {// Catch exception if any
					System.err.println("Error: " + e1.getMessage());
				}
			}
		}

	}
}
